﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Threading;

namespace _2DRPG
{
    class GEngine
    {        
        /* ------------- Class members -------------*/
        private Graphics drawHandle;
        private Thread renderThread;

        /* ------------- Class methods -------------*/
        public GEngine(Graphics g)
        {
            drawHandle = g;
        }

        public void init()
        {
            renderThread = new Thread(new ThreadStart(render));
            renderThread.Start();
        }

        public void stop()
        {
            renderThread.Abort();
        }

        private void render()
        {
            int framesRendered = 0;
            long startTime = Environment.TickCount;
            
            Bitmap frame = new Bitmap(Game.CANVAS_WIDTH, Game.CANVAS_HEIGHT);
            Graphics frameGraphics = Graphics.FromImage(frame);


            while (true)
            {
                if (drawHandle != null)
                {   
                    frameGraphics.FillRectangle(new SolidBrush(Color.Aqua), 0, 0, Game.CANVAS_WIDTH, Game.CANVAS_HEIGHT);
                    drawHandle.DrawImage(frame, 0, 0);
   
                    // benchmarking
                    framesRendered++;

                    if (Environment.TickCount >= (startTime + 1000))
                    {
                        Console.WriteLine("GEngine: " + framesRendered + " fps");
                        framesRendered = 0;
                        startTime = Environment.TickCount;
                    }
                }                
            }
        }
    }
}
